package com.etc.service.impl;

import com.etc.dao.DoctorMapper;
import com.etc.dao.OrderMapper;
import com.etc.dao.TimeMapper;
import com.etc.dto.CountVO;
import com.etc.entity.Doctor;
import com.etc.entity.Order;
import com.etc.entity.Time;
import com.etc.service.OrderService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.joda.time.DateTime;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class OrderServiceImpl implements OrderService {
    private static final Logger logger = LoggerFactory.getLogger(OrderServiceImpl.class);

    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private TimeMapper timeMapper;
    @Autowired
    private DoctorMapper doctorMapper;


    @Override
    public List<Order> finOrderDetailByDoctorId(String doctorId) {
        Order order = new Order();
        order.setDoctorId(doctorId);

        return orderMapper.findByOrderdetail(order);
    }

    @Override
    public PageInfo<Order> finOrderDetailByDoctorIdByPage(String doctorId, Integer pageNum, Integer pageSize) {

        //调用持久层
        Order order = new Order();
        order.setDoctorId(doctorId);
        List<Order> orderList =  orderMapper.findByOrderdetailByDoctorIdAsc(order);
        //开始分页
        PageHelper.startPage(pageNum,pageSize);


        //创建新的存储对象
        List<Order> orderList1 = new ArrayList();


        for (Order order1 : orderList) {
            if (order1.getOrderTime().substring(0, 10).equals(LocalDate.now().toString().substring(0, 10))) {

                orderList1.add(order1);
            }

        }
        //封装pageinfo
        PageInfo<Order> pageInfo = new PageInfo(orderList1);

        return pageInfo;
    }

    @Override
    public PageInfo<Order> findOrderDetail(Order order, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Order> orderList = orderMapper.findByOrderdetail(order);
        PageInfo<Order> pageInfo = new PageInfo<>(orderList);
        return pageInfo;
    }

    @Override
    public List<Order> findOrderPatientDoctorPayList(Order order) {
        List<Order> orderPatientDoctorPayList = orderMapper.findOrderPatientDoctorPayList(order);
        return orderPatientDoctorPayList;
    }

    @Override
    public List<CountVO> getCount() {
        List countList=new ArrayList();
        List<Doctor> doctorList = doctorMapper.findAllSelective(null);
        for (Doctor doc : doctorList) {
            String doctorId = doc.getDoctorId();
            String doctorName=doc.getDoctorName();
            Integer count = orderMapper.Count(doctorId);
            CountVO countVO=new CountVO();
            countVO.setName(doctorName);
            countVO.setValue(count);
            countList.add(countVO);
        }
        return countList;
    }

    @Override
    public List<Order> getPayAndDetailList(String patientId) {
        Order order=new Order();
        order.setPatientId(patientId);
        List<Order> payAndDetailList = orderMapper.findPayAndDetailList(order);
        return payAndDetailList;
    }

    @Override
    public List<Order> getPatientByPatientId(Order order) {
        List<Order> orderList =orderMapper.findByOrderdetail(order);
        return orderList;
    }

    @Override
    public List<Order> getOrdersByDoctorId(String doctorId, String firstDay) {
        //设置第七天
        String lastDay = new DateTime(firstDay).plusDays(6).toString("yyyy-MM-dd");
        //先获取时间范围内的排班对象列表
        List<Time> times = timeMapper.findTimeByDoctorIdAndDate(doctorId, firstDay, lastDay);
        //遍历times，调用orderMapper获取该医生所有的预约对象
        List<Order> orders = new ArrayList<>();
        for(Time t:times){
            String start = t.getContent();
            String end = start.replaceAll("00:00:00", "23:59:59");
            List<Order> orderList = orderMapper.findOrderByDoctorAndRange(t.getDoctorId(), start, end);
            orders.addAll(orderList);
        }



        return orders;
    }

    @Override
    public boolean updateOrderStatusByOrderId(Order order) {

        return orderMapper.updateBykey(order);
    }

    @Override
    public boolean addOrder(List<Order> orders) {
        boolean b = orderMapper.insertOrderList(orders);
        return b;
    }

    @Override
    public PageInfo<Order> findOrderPatientDoctorList(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Order> orderList = orderMapper.findOrderPatientDoctorList();
        PageInfo<Order> pageInfo = new PageInfo<>(orderList);
        return pageInfo;
    }


}
